package com.example.li.utils;

import android.os.Handler;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 文件下载 (下载进度)
 */
public class OkHttpUtils {
    private final static OkHttpClient okHttpClient = new OkHttpClient();

    private OkHttpUtils() {

    }

    private final static Handler h = new Handler() {
    };

    abstract static class OnOkHttp {
        abstract void onFailure(IOException e);

        abstract void onResponse(String address);

        abstract void onProgress(float progress);
    }

    public static void fileDown(String url, final String dir, final String fileName, final OnOkHttp onOkHttp) {
        Request build = new Request.Builder().url(url).build();
        okHttpClient.newCall(build).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                h.post(new Runnable() {
                    @Override
                    public void run() {
                        onOkHttp.onFailure(e);
                    }
                });
            }

            @Override
            public void onResponse(Call call, final Response response) {
                InputStream is = response.body().byteStream();
                final File file = new File(dir, fileName);
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(file);
                    byte[] buf = new byte[1024];
                    final long max = response.body().contentLength();
                    int line = -1;
                    long sum = 0;
                    while ((line = is.read(buf)) != -1) {
                        fos.write(buf, 0, line);
                        sum += line;
                        final float p = 1.0f * sum / max;
                        h.post(new Runnable() {
                            @Override
                            public void run() {
                                onOkHttp.onProgress(p);
                            }
                        });
                    }
                    h.post(new Runnable() {
                        @Override
                        public void run() {
                            onOkHttp.onResponse(file.getAbsolutePath());
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fos != null) {
                        try {
                            fos.flush();
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        });
    }
}
